package ijij41.togetherTrain.common;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ijij41.chat.ChatActivity;
import ijij41.chat.MultiChatActivity;
import ijij41.network.NetworkRequester;
import ijij41.togetherTrain.Constants;
import ijij41.togetherTrain.MainActivity;
import ijij41.togetherTrain.R;
import ijij41.togetherTrain.config.TrainConfig;
import ijij41.togetherTrain.participate.RoomListActivity;
import ijij41.togetherTrain.resource.MyTicketRoomListRow;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class RoomViewActivity extends Activity {

	private String room_idx;
	private boolean isParticipate;

	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.room_view);

		initComponent();

	}

	private void initComponent() {
		getParameters();
		setInitialValue();
		setHandler();
	}

	private void setHandler() {

		findViewById(R.id.btn_back_img).setOnClickListener(mClickListener);
		findViewById(R.id.btn_control_in_room).setOnClickListener(mClickListener);
		findViewById(R.id.btn_chat_start).setOnClickListener(mClickListener);
		
		
	}

	private void setInitialValue() {

		TextView array_tv_user[] = new TextView[4];
		array_tv_user[0] = ((TextView) findViewById(R.id.tv_0_user));
		array_tv_user[1] = ((TextView) findViewById(R.id.tv_1_user));
		array_tv_user[2] = ((TextView) findViewById(R.id.tv_2_user));
		array_tv_user[3] = ((TextView) findViewById(R.id.tv_3_user));

		JSONObject obj = NetworkRequester.getInstance().getRoomView(room_idx);

		try {
			((TextView) findViewById(R.id.tv_room_num)).setText(obj
					.getString("room_num"));

			((TextView) findViewById(R.id.tv_num_member)).setText(obj
					.getString("current_num_member")
					+ "/"
					+ obj.getString("max_num_member"));

			((TextView) findViewById(R.id.tv_date)).setText(obj
					.getString("date"));

			((TextView) findViewById(R.id.tv_num_train)).setText(obj
					.getString("num_train"));

			((TextView) findViewById(R.id.tv_start_time)).setText(obj
					.getString("start_time"));
			((TextView) findViewById(R.id.tv_start_station)).setText(obj
					.getString("start_station"));
			((TextView) findViewById(R.id.tv_dest_time)).setText(obj
					.getString("dest_time"));
			((TextView) findViewById(R.id.tv_dest_station)).setText(obj
					.getString("dest_station"));
			
			

			JSONArray jsonArray = new JSONArray(obj.getString("user_info"));
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject userinfo = new JSONObject(jsonArray.getString(i));
				array_tv_user[i].setText(userinfo.getString("id"));
				if (userinfo.getString("id").equals(TrainConfig.user_id)) {
					isParticipate = true;
				}
			}

			if (isParticipate) {
				((Button) findViewById(R.id.btn_control_in_room))
						.setText("방 나가기");
			} else {
				((Button) findViewById(R.id.btn_control_in_room))
						.setText("방 참여하기");
			}

			// TODO 방나갈때, 방장권한 넘긴다는 메세지, 진짜 나가도 되는지 여부의 확인창 필요

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void getParameters() {
		// get parameters from before activity
		Intent intent = getIntent();
		room_idx = intent.getStringExtra("room_idx");
		// ///////////////////////

	}

	private Button.OnClickListener mClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btn_back_img:
				finish();
				break;

			case R.id.btn_control_in_room:
				if (isParticipate) {
					leaveRoom();
				} else {
					participateRoom();
				}

				break;
				
			case R.id.btn_chat_start:
				
				Intent MultiChatActivity = new Intent(RoomViewActivity.this,
						MultiChatActivity.class);
				
				MultiChatActivity.putExtra("room_idx", room_idx);
				startActivity(MultiChatActivity);	

				
				break;
			}

		}

		
		//TODO 참여/불참여 중 참여시에만 챗팅창이 보이게 한다.
		
		//TODO 레디 표시를 할 경우, 레디 checkbox의 크기 조절 http://www.anddev.org/tutorial_change_look_of_checkbox-t4553.html
		
		
		private void participateRoom() {
			int returnCode = NetworkRequester.getInstance().participateRoom(
					room_idx, TrainConfig.user_id);			
			if (returnCode == 1) {
				Toast.makeText(RoomViewActivity.this, "방에 정상 참여 하였습니다.",
						Toast.LENGTH_SHORT).show();
				setInitialValue();   //TODO 실제 참여 하면, back버튼 눌렀을때도, 내용 반영 되게 해야 한다. (???)
			} else {
				//if (returnCode == -1) {
					Toast.makeText(RoomViewActivity.this,
							"방을 참여 하는데 문제가 생겼습니다", Toast.LENGTH_SHORT).show();
				//}
			}
		}
		
		

		private void leaveRoom() {
			int returnCode = NetworkRequester.getInstance().leaveRoom(room_idx,
					TrainConfig.user_id);
					
			if(returnCode ==1 ) {
				Toast.makeText(RoomViewActivity.this, "방을 탈퇴하였습니다.",
						Toast.LENGTH_SHORT).show();
				finish();
			} else {
				Toast.makeText(RoomViewActivity.this, "방을 탈퇴하는데 문제가 생겼습니다",
						Toast.LENGTH_SHORT).show();
			}

		}
	};
}
